package com.sxt.system.controller;

import com.sxt.system.common.Constast;
import com.sxt.system.common.DataGridView;
import com.sxt.system.common.ResultObj;
import com.sxt.system.domain.Notice;
import com.sxt.system.service.NoticeService;
import com.sxt.system.utils.AppWebUtils;
import com.sxt.system.vo.NoticeVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName NoticeController
 * @Description TODO
 * @Author 雷哥
 * @Date 2020/3/6 17:29
 */
@RestController
@RequestMapping(Constast.API+"notice")
public class NoticeController extends  BaseController{

    @Autowired
    private NoticeService noticeService;


    /***
     * 全查询
     */
    @GetMapping("loadAllNotice")
    public DataGridView loadAllNotice(NoticeVo noticeVo){
        return this.noticeService.queryAllNotice(noticeVo);
    }


    /**
     * 添加
     */
    @PostMapping("addNotice")
    public ResultObj addNotice(Notice notice){
        try {
            notice.setOpername(AppWebUtils.getCurrentUserName());
            notice.setCreatetime(new Date());
            this.noticeService.save(notice);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            log.error("添加失败",e);
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改
     */
    @PostMapping("updateNotice")
    public ResultObj updateNotice(Notice notice){
        try {
            if(notice.getId()!=null){
                notice.setOpername(AppWebUtils.getCurrentUserName());
                notice.setCreatetime(new Date());
                this.noticeService.save(notice);
                return ResultObj.UPDATE_SUCCESS;
            }else{
                log.error("修改失败.id不能为空");
                return ResultObj.UPDATE_ERROR;
            }
        }catch (Exception e){
            log.error("修改失败",e);
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @PostMapping("deleteNotice")
    public ResultObj deleteNotice(Integer id){
        try {
            if(id!=null){
                this.noticeService.removeById(id);
                return ResultObj.DELETE_SUCCESS;
            }else{
                log.error("删除失败.id不能为空");
                return ResultObj.DELETE_ERROR;
            }
        }catch (Exception e){
            log.error("删除失败",e);
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     */
    @PostMapping("batchDeleteNotice")
    public ResultObj batchDeleteNotice(Integer[] ids){
        try {
            if(ids!=null&&ids.length>0){

                List<Integer> listIds=new ArrayList<>(ids.length);
                for (Integer id : ids) {
                    listIds.add(id);
                }
                this.noticeService.removeByIds(listIds);
                return ResultObj.DELETE_SUCCESS;
            }else{
                log.error("删除失败.id数组不能为空");
                return ResultObj.DELETE_ERROR;
            }
        }catch (Exception e){
            log.error("删除失败",e);
            return ResultObj.DELETE_ERROR;
        }
    }



}
